package max.ta.data.plot.gen;



import java.util.ArrayList;
import java.util.List;

import max.ta.data.Function;
import max.ta.data.plot.data.MathPoint;


public class RealDataGenerator
	extends DataGenerator<Double, Double> {

    private final Function<Double, Double> function;
    private final Double minX;
    private final Double maxX;
    private final List<MathPoint<Double, Double>> points = new ArrayList<MathPoint<Double, Double>>();

    public RealDataGenerator(Function<Double, Double> function, final Double minX, final Double maxX) {
	super();
	this.function = function;
	this.minX = minX;
	this.maxX = maxX;
    }


    @Override
    public List<MathPoint<Double, Double>> generate() {
	final double delta = (maxX - minX) / MAX_POINTS;
	points.clear();
	for (int i = 0; i < MAX_POINTS; i++) {
	    final double x = points.isEmpty() ? minX : points.get(i - 1).getX() + delta;
	    final double y = function.compute(x);
	    final MathPoint<Double, Double> point = new MathPoint<Double, Double>(x, y);
	    points.add(point);
	}
	return points;
    }


    public static Double[][] toArray(final List<MathPoint<Double, Double>> data) {
	final Double[][] result = new Double[2][data.size()];
	for (int i = 0; i < data.size(); i++) {
	    final MathPoint<Double, Double> point = data.get(i);
	    result[0][i] = point.getX();
	    result[1][i] = point.getY();
	}
	return result;
    }


}
